package org.nlp.lucene.store;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.util.Version;

public class TestRAMDirectory {
	public static void main(String[] args) throws IOException {
		Version MatchVersion = Version.LUCENE_40;
		RAMDirectory dir = new RAMDirectory();
	    IndexWriter ramwrite = new IndexWriter(dir, new IndexWriterConfig(
	    		MatchVersion, new StandardAnalyzer(MatchVersion)));
	        // add some documents
	        Document doc = null;
	        int docsToAdd = 3;
	        
	        //write
			for (int i = 0; i < docsToAdd; i++) {
	          doc = new Document();
	          doc.add(new StringField("content", "english"+i, Field.Store.YES));
	          ramwrite.addDocument(doc);
	        } 
			ramwrite.commit();
			//read
			IndexReader reader = DirectoryReader.open(dir);
			System.out.println("numDocs:"+reader.numDocs());
			IndexSearcher searcher = new IndexSearcher(reader);
		    for (int i = 0; i < docsToAdd; i++) {
		        doc = searcher.doc(i);
		        System.out.println(doc.getField("content"));
		      }
		    reader.close();
	}
}
